package com.ruoyi.web.qualityController;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.quality.QualityDefectDefinition;
import com.ruoyi.system.mapper.QualityDefectDefinitionMapper;
import com.ruoyi.system.service.IQualityDefectDefinitionService;
import com.ruoyi.system.service.IQualityDefectTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 质量缺陷定义Controller
 *
 * @author ruoyi
 * @date 2023-03-07
 */
@Api(tags = "质量缺陷定义Controller")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/quality/definition" )
public class QualityDefectDefinitionController extends BaseController {

    private final IQualityDefectDefinitionService iQualityDefectDefinitionService;

    @Autowired
    private IQualityDefectTypeService typeService;

    @Resource
    private QualityDefectDefinitionMapper qualityDefectDefinitionMapper;

    /**
     * 查询质量缺陷定义列表
     */
    //@PreAuthorize("@ss.hasPermi('quality:definition:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询质量缺陷定义列表")
    public TableDataInfo list(QualityDefectDefinition qualityDefectDefinition)
    {
        startPage();
        LambdaQueryWrapper<QualityDefectDefinition> lqw = new LambdaQueryWrapper<QualityDefectDefinition>();
        if (StringUtils.isNotBlank(qualityDefectDefinition.getCode())){
            lqw.apply("a.code like '%"+qualityDefectDefinition.getCode()+"%'");
        }
        if (StringUtils.isNotBlank(qualityDefectDefinition.getName())){
            lqw.apply("a.name like '%"+qualityDefectDefinition.getName()+"%'");
        }
        if (qualityDefectDefinition.getTypeId() != null){
            lqw.eq(QualityDefectDefinition::getTypeId ,qualityDefectDefinition.getTypeId());
        }
        if (qualityDefectDefinition.getDeptId() != null){
            lqw.eq(QualityDefectDefinition::getDeptId ,qualityDefectDefinition.getDeptId());
        }
        if (qualityDefectDefinition.getDelId() != null){
            lqw.eq(QualityDefectDefinition::getDelId ,qualityDefectDefinition.getDelId());
        }
        if (qualityDefectDefinition.getTypeName() != null){
            lqw.apply("c.name like '%"+qualityDefectDefinition.getTypeName()+"%'");
        }
        if (qualityDefectDefinition.getDeptName() != null){
            lqw.apply("d.dept_name like '%"+qualityDefectDefinition.getDeptName()+"%'");
        }
        if (qualityDefectDefinition.getDelName() != null){
            lqw.apply("b.name like '%"+qualityDefectDefinition.getDelName()+"%'");
        }
        if (StringUtils.isNotBlank(qualityDefectDefinition.getUnit())){
            lqw.like(QualityDefectDefinition::getUnit ,qualityDefectDefinition.getUnit());
        }
        if (StringUtils.isNotBlank(qualityDefectDefinition.getLocation())){
            lqw.like(QualityDefectDefinition::getLocation ,qualityDefectDefinition.getLocation());
        }

        if (StringUtils.isNotBlank(qualityDefectDefinition.getAlias())){
            lqw.like(QualityDefectDefinition::getAlias ,qualityDefectDefinition.getAlias());
        }
        List<QualityDefectDefinition> list = qualityDefectDefinitionMapper.selectByParam(lqw);

        return getDataTable(list);
    }

    /**
     * 导出质量缺陷定义列表
     */
    //@PreAuthorize("@ss.hasPermi('quality:definition:export')" )
    @Log(title = "质量缺陷定义" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    @ApiOperation(value = "导出质量缺陷定义列表")
    public AjaxResult export(QualityDefectDefinition qualityDefectDefinition) {
        LambdaQueryWrapper<QualityDefectDefinition> lqw = new LambdaQueryWrapper<QualityDefectDefinition>(qualityDefectDefinition);
        List<QualityDefectDefinition> list = iQualityDefectDefinitionService.list(lqw);
        ExcelUtil<QualityDefectDefinition> util = new ExcelUtil<QualityDefectDefinition>(QualityDefectDefinition. class);
        return util.exportExcel(list, "definition" );
    }

    /**
     * 获取质量缺陷定义详细信息
     */
    //@PreAuthorize("@ss.hasPermi('quality:definition:query')" )
    @GetMapping(value = "/{id}" )
    @ApiOperation(value = "获取质量缺陷定义详细信息")
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iQualityDefectDefinitionService.getById(id));
    }

    /**
     * 新增质量缺陷定义
     */
    //@PreAuthorize("@ss.hasPermi('quality:definition:add')" )
    @Log(title = "质量缺陷定义" , businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增质量缺陷定义")
    public AjaxResult add(@RequestBody QualityDefectDefinition qualityDefectDefinition) {
        typeService.checkUnique("quality_defect_definition",qualityDefectDefinition.getCode(),null);
        return toAjax(iQualityDefectDefinitionService.save(qualityDefectDefinition) ? 1 : 0);
    }

    /**
     * 修改质量缺陷定义
     */
    //@PreAuthorize("@ss.hasPermi('quality:definition:edit')" )
    @Log(title = "质量缺陷定义" , businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改质量缺陷定义")
    public AjaxResult edit(@RequestBody QualityDefectDefinition qualityDefectDefinition) {
        typeService.checkUnique("quality_defect_definition",qualityDefectDefinition.getCode(), qualityDefectDefinition.getId());
        return toAjax(iQualityDefectDefinitionService.updateById(qualityDefectDefinition) ? 1 : 0);
    }

    /**
     * 删除质量缺陷定义
     */
    //@PreAuthorize("@ss.hasPermi('quality:definition:remove')" )
    @Log(title = "质量缺陷定义" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    @ApiOperation(value = "删除质量缺陷定义")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iQualityDefectDefinitionService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
